package com.overstock.flightcalc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class FlightCalculatorActivity extends Activity {

	private static String TAG = "flight-calculator-example";
	private final String RESTORE = ", can restore state";

	// a string as an example of state
	private final String state = "fubar";

	/**
	 * Called when the activity is first created.
	 * 
	 * The onCreate method is called after an Activity instance has been
	 * created. This is where most applications perform most of their
	 * initialization: reading in the layouts and creating View instances,
	 * binding to data, and so on. Note that, if this Activity instance has not
	 * been destroyed, nor the process killed, this is not called again. It is
	 * called only if a new instance of an Activity class is created. The
	 * argument to this method is a Bundle object that contains saved
	 * application state. If there is no saved state, the value of this argument
	 * is null.
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it
	 *            is null.</b>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
		setContentView(R.layout.main);
		String answer = null;
		// savedState could be null
		if (null != savedInstanceState) {
			answer = savedInstanceState.getString("answer");
		}
		Log.i(TAG, "onCreate"
				+ (null == savedInstanceState ? "" : (RESTORE + " " + answer)));
	}

	/**
	 * The onRestart method is called only if an activity has been stopped.
	 * “Stopped” means the activity is not in the foreground, interacting with
	 * the user. This method is called before the onStart method.
	 */
	@Override
	protected void onRestart() {
		super.onRestart();
		// Notification that the activity will be started
		Log.i(TAG, "onRestart");
	}

	/**
	 * The onStart method is called when the Activity object and its views
	 * become visible to the user.
	 */
	@Override
	protected void onStart() {
		super.onStart();
		// Notification that the activity is starting
		Log.i(TAG, "onStart");
	}

	/**
	 * The onResume method is called when the Activity object and its views
	 * become interactive with the user.
	 */
	@Override
	protected void onResume() {
		super.onResume();
		// Notification that the activity will interact with the user
		Log.i(TAG, "onResume");
	}

	/**
	 * The onPause method is called when a different Activity instance is going
	 * to be visible and the current Activity has stopped interacting with the
	 * user.
	 */
	@Override
	protected void onPause() {
		super.onPause();
		// Notification that the activity will stop interacting with the user
		Log.i(TAG, "onPause" + (isFinishing() ? " Finishing" : ""));
	}

	/**
	 * The onStop method is called when an activity is no longer visible to, or
	 * interacting with, the user.
	 */
	@Override
	protected void onStop() {
		super.onStop();
		// Notification that the activity is no longer visible
		Log.i(TAG, "onStop");
	}

	/**
	 * The onDestroy method is called when an Activity instance is going to be
	 * destroyed— no longer used. Before this method is called, the activity has
	 * already stopped interacting with the user and is no longer visible on the
	 * screen. If this method is being called as the result of a call to finish,
	 * a call to isFinishing will return true.
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Notification the activity will be destroyed
		Log.i(TAG, "onDestroy "
		// Log which, if any, configuration changed
				+ Integer.toString(getChangingConfigurations(), 16));
	}

	// ////////////////////////////////////////////////////////////////////////////
	// Called during the lifecycle, when instance state should be saved/restored
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * This is where an application gets a chance to save instance state.
	 * Instance state should be state that is not persisted with an
	 * application’s data model, such as the state of an indicator or other
	 * state that is only part of the Activity object. This method has an
	 * implementation in the parent class: it calls the onSaveInstance State
	 * method of each View object in this instance of Activity, which has the
	 * result of saving the state of these View objects, and this is often the
	 * only state you need to store this way. Data that your subclass needs to
	 * store is saved using the “put” methods of the Bundle class.
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// Save instance-specific state
		outState.putString("answer", state);
		super.onSaveInstanceState(outState);
		Log.i(TAG, "onSaveInstanceState");

	}

	/**
	 * The onRetainNonConfigurationInstance method is called after onStop, which
	 * means there is no guarantee it will be called, nor even, if it is called,
	 * that the reference returned will be preserved and provided to the
	 * subsequent Activity instance. The getLastNonConfigurationInstance()
	 * method can be called in the onCreate method, or subsequently when
	 * restoring activity state.
	 */
	@Override
	public Object onRetainNonConfigurationInstance() {
		Log.i(TAG, "onRetainNonConfigurationInstance");
		return new Integer(getTaskId());
	}

	/**
	 * The onRestoreInstanceState method is called when there is instance state
	 * to be restored. If this method is called, it is called after onStart and
	 * before onPostCreate, which is a minor life cycle method.
	 */
	@Override
	protected void onRestoreInstanceState(Bundle savedState) {
		super.onRestoreInstanceState(savedState);
		// Restore state; we know savedState is not null
		String answer = null != savedState ? savedState.getString("answer")
				: "";
		Object oldTaskObject = getLastNonConfigurationInstance();
		if (null != oldTaskObject) {
			int oldtask = ((Integer) oldTaskObject).intValue();
			int currentTask = getTaskId();
			// Task should not change across a configuration change
			assert oldtask == currentTask;
		}
		Log.i(TAG, "onRestoreInstanceState"
				+ (null == savedState ? "" : RESTORE) + " " + answer);
	}

	// ////////////////////////////////////////////////////////////////////////////
	// These are the minor lifecycle methods, you probably won't need these
	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * The onPostCreate method is called after onRestoreInstanceState is called.
	 * It may be useful if your application requires that state be restored in
	 * two stages. It is passed a Bundle object containing instance state.
	 * 
	 */
	@Override
	protected void onPostCreate(Bundle savedState) {
		super.onPostCreate(savedState);
		String answer = null;
		// savedState could be null
		if (null != savedState) {
			answer = savedState.getString("answer");
		}
		Log.i(TAG, "onPostCreate"
				+ (null == savedState ? "" : (RESTORE + " " + answer)));

	}

	/**
	 * The onPostResume method is called after onResume, when the Activity
	 * instance should be visible and is interacting with the user.
	 */
	@Override
	protected void onPostResume() {
		super.onPostResume();
		Log.i(TAG, "onPostResume");
	}

	/**
	 * The onUserLeaveHint method is called if an activity is going to stop
	 * being visible and interacting with the user due to the user’s actions—for
	 * example, pressing the back or home hard keys. This is a convenient place
	 * to clear alerts and dialogs.
	 */
	@Override
	protected void onUserLeaveHint() {
		super.onUserLeaveHint();
		Log.i(TAG, "onUserLeaveHint");
	}

}
